package development;

import iplayparticle.view.iplayparticleResources;
import state.GameConstants;
import state.GameImage;
import state.Graphics;
import state.Manager;
import state.component.BottomBar;
import state.font.FontManager;
import state.font.ImageFont;
import state.util.GameColor;
import state.util.ImageUtil;

public class DrawUtils {
  private static GameImage lightImage;
  private static GameImage topBarImage;

  private static GameImage hozFrameImage;
  private static GameImage bottomBarImage;

  private static BottomBar bottomBar;

  public static void drawBackground(Graphics g) {
    drawBackground(g, 0x7C0000);
  }
  
  public static void drawAppBackground(Graphics g) {
    g.setColor(0x1482B8);
    g.fillRect(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
  }

  private static void drawBackground(Graphics g, int color) {
    if ((Global.authenClient != null) && Global.authenClient.isConnectSuccess) {
      if (lightImage == null) {
        lightImage = ImageUtil.getImage(Constants.NEW_CORE_IMAGE_CATEGORY, "Light-on-main.png");
        lightImage.setStatic(true);
      }
    }
    
    g.setColor(color);
    g.fillRect(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
    
    if (lightImage != null) {
      if (Constants.IS_240x320_SCREEN) {
        g.drawImage(lightImage, Constants.SCREEN_WIDTH / 2, 160, Constants.BOTTOM_HCENTER_ANCHOR);
      } else {
        g.drawImage(lightImage, Constants.SCREEN_WIDTH / 2, 120, Constants.BOTTOM_HCENTER_ANCHOR);
      }
    }
  }
  
  public static BottomBar getCurrentBottomBar() {
    return bottomBar;
  }
  
  public static void setBottomBar(Manager manager, String leftLabel, String centerLabel, String rightLabel) {
    bottomBar = getBottomBar(manager);
    bottomBar.setLabels(leftLabel, centerLabel, rightLabel);
  }
  
  public static BottomBar getBottomBar(Manager manager) {
    if (bottomBar == null) {
      bottomBar = new BottomBar(manager, 25);
      if (bottomBarImage == null) {
        bottomBarImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "bg_footer.png");
        bottomBarImage = ImageUtil.joinAndCreateImages(bottomBarImage, Constants.SCREEN_WIDTH, 25);
        bottomBarImage.setStatic(true);
      }
    } else {
      if (bottomBar.getManager() != manager && (manager != null)) {
        bottomBar.setManager(manager);
        bottomBar.reset();
      }
    }
    bottomBar.setImage(bottomBarImage);
    bottomBar.setForceGroundColor(GameColor.WHITE_CODE);
    return bottomBar;
  }

  public static void drawTopBar(Graphics g) {
    if (topBarImage == null) {
      topBarImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "bg_top.png");
      topBarImage = ImageUtil.joinAndCreateImages(topBarImage, Constants.SCREEN_WIDTH, 25);
      topBarImage.setStatic(true);
    }
    g.drawImage(topBarImage, 0, 0, Constants.TOP_LEFT_ANCHOR);
  }

  public static GameImage getMenuItemBackground(int width) {
    GameImage menuItemBackground = new GameImage(iplayparticleResources.image_bg_menu, null, null);
    menuItemBackground = ImageUtil.joinAndCreateImages(menuItemBackground, width, 20);
    menuItemBackground.setStatic(true);
    return menuItemBackground;
  }

  public static void drawHozPopup(Graphics g, int width, int x, int y) {
    if (hozFrameImage == null) {
      hozFrameImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "bg_chat.png");
      hozFrameImage.setStatic(true);
    }
    g.drawImage(hozFrameImage, Constants.SCREEN_WIDTH / 2, Constants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
  }

  public static void drawNotification(Graphics g, String text, ImageFont font) {
    int width = font.stringWidth(text);
    g.setColor(0xFFA500);
    g.fillRoundRect((Constants.SCREEN_WIDTH - width) / 2 , GameConstants.SCREEN_HEIGHT / 2 - 22, width, 32, 5, 5);
    font.setBold(true);
    font.drawString2(g, text, 0xffffff, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2 - 16, GameConstants.TOP_HCENTER_ANCHOR);
    font.setBold(false);
  }

  public static void drawUserInfo(Graphics g, String username, int x, int y, boolean isRightToLeft) {
    ImageFont text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    int anchor = isRightToLeft ? GameConstants.TOP_RIGHT_ANCHOR : GameConstants.TOP_LEFT_ANCHOR;
    g.drawImage(Global.getAvatar(username), x, y, anchor);

    text8.drawString2(g, username, GameColor.WHITE_CODE, x, y + 35, anchor);
    if (username.equals(Global.currentUser.getName())) {
      text8.drawString2(g, Global.currentUser.getGold() + " coin", GameColor.YELLOW_CODE, x, y + 50, anchor);
      g.setColor(GameColor.YELLOW_CODE);
    }
  }

  public static int getTopBarHeight() {
    return 25;
  }

  public static int getBottomBarHeight() {
    if (bottomBar == null) {
      bottomBar = getBottomBar(Global.systemCanvas.frmCurrent.getManager());
    }
    return bottomBar.getHeight();
  }
}
